//
//  SVGTextElement.m
//  Shake and Bake
//
//  Created by Antoine Quint on 19/10/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "SVGTextElement.h"

@implementation SVGTextElement

- (id)initWithAttributeDict:(NSDictionary *)attributesDict andView:(Canvas *)aView
{
  [super initWithAttributeDict:attributesDict andView:aView];
	if (self) {
		// parse attribute values
		x = [[attributes objectForKey:@"x"] floatValue];
		y = [[attributes objectForKey:@"y"] floatValue];
		fontSize = [[attributes objectForKey:@"font-size"] floatValue];
		fontFamily = [attributes objectForKey:@"font-family"];
		//
		self.position = CGPointMake(x - (strokeWidth / 2), y - (strokeWidth / 2)); 
    self.bounds = CGRectMake(0.0f, 0.0f, 200.0f, 100.0f);
		self.name = @"text";
    // flip the transform again
    self.transform = CATransform3DScale(self.transform, 1.0f, -1.0f, 1.0f);
		// set up textContent to be empty
    textContent = [NSMutableString string];
	}
	return self;
}

- (void)drawInContext:(CGContextRef)context
{
  CGContextSelectFont(context, [fontFamily UTF8String], fontSize, kCGEncodingMacRoman);
  // figure out the drawing mode
	CGTextDrawingMode mode = kCGTextInvisible;
	if (fillColor && strokeColor) {
    mode = kCGTextFillStroke;
  } else if (fillColor && strokeColor == nil) {
    mode = kCGTextFill;
  } else if (fillColor == nil && strokeColor) {
    mode = kCGTextStroke;
  }
  CGContextSetTextDrawingMode(context, mode); 
  // fill, if needed
  if (fillColor) {
  	CGContextSetRGBFillColor(context, [fillColor redComponent], [fillColor greenComponent], [fillColor blueComponent], fillOpacity); 
  }
  // stroke, if needed
  if (strokeColor) {
		CGContextSetRGBStrokeColor(context, [strokeColor redComponent], [strokeColor greenComponent], [strokeColor blueComponent], strokeOpacity);
  }
  // display the text
  CGContextShowTextAtPoint(context, 0, 0, [textContent UTF8String], [textContent length]); 
}

- (void)addToTextContent:(NSString *)characters
{
  [textContent appendString:characters];
}

@end
